package com.pfizer.pfizerExpertForPad;

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.pfizer.DB.DataHelper;
import com.pfizer.Entity.Question;
import com.pfizer.Entity.QuestionOption;
import com.pfizer.Entity.UserLog;

/**
 * HandoutTestActivity--<b>小測驗</b>--
 * 
 * @author krui
 * @version 2.0 2011/11/23
 * 
 * @see android.view.View.OnClickListener
 */
public class HandoutTestActivity extends Activity implements OnClickListener {

	// 定義相關變量
	private DataHelper dHelper = null;
	pfizerApplication pApplication = null;
	Context mContext = null;
	TextView tvQuestionTitle = null;
	Button btnNext = null;
	List<Question> questionList = null;// 問題數據源List
	RadioGroup rgChoose = null;
	RadioButton rbChoose1 = null;
	RadioButton rbChoose2 = null;
	RadioButton rbChoose3 = null;
	RadioButton rbChoose4 = null;
	TextView tvInfo = null;
	Question question;
	List<QuestionOption> optionList = null;
	int iLength = 0;
	int iCurrItem = 0;
	long qnID = 0;
	String ClassID = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);// 設置無標題
		setContentView(R.layout.testdialog);
		mContext = HandoutTestActivity.this;
		InitApp initApp = new InitApp(mContext);
		initApp.SetApp(0);
		pApplication = (pfizerApplication) getApplicationContext();
		dHelper = pApplication.getDataHelper();
		Intent intent = this.getIntent();
		ClassID = intent.getStringExtra("ClassID");
		qnID = intent.getLongExtra("QNID", 0);
		if (qnID == 0) {
			finish();
		}
		// 已經同步好的數據，從數據庫中讀取顯示
		questionList = dHelper.getQuestionList(qnID);
		iLength = questionList.size();
		if (iLength == 0) {
			finish();
		}
		iCurrItem = 0;
		tvQuestionTitle = (TextView) this.findViewById(R.id.tvQuestionTitle);
		rgChoose = (RadioGroup) this.findViewById(R.id.rgChoose);
		rbChoose1 = (RadioButton) this.findViewById(R.id.rbChoose1);
		rbChoose2 = (RadioButton) this.findViewById(R.id.rbChoose2);
		rbChoose3 = (RadioButton) this.findViewById(R.id.rbChoose3);
		rbChoose4 = (RadioButton) this.findViewById(R.id.rbChoose4);
		tvInfo = (TextView) this.findViewById(R.id.tvInfo);
		btnNext = (Button) this.findViewById(R.id.btnNext);
		btnNext.setOnClickListener(this);
		showQuestion();
	}

	/**
	 * 各個空間的事件處理
	 */
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		// dHelper.updateMessageFlag(msg.getAutoID(), 2);
		tvInfo.setVisibility(View.GONE);
		int UserAswer = -1;
		// 判斷用戶選擇
		if (rbChoose1.isChecked()) {
			UserAswer = 0;
		} else if (rbChoose2.isChecked()) {
			UserAswer = 1;
		} else if (rbChoose3.isChecked()) {
			UserAswer = 2;
		} else if (rbChoose4.isChecked()) {
			UserAswer = 3;
		}
		if(UserAswer==-1)
		{
			tvInfo.setVisibility(View.VISIBLE);
			return;
		}
		//如果回答錯誤，就繼續測驗直到正確，才能進行下一題測驗
		if (!optionList.get(UserAswer).getIsCorrect()) {
			tvInfo.setVisibility(View.VISIBLE);
			return;
		}
		//回答問題完畢時的處理
		if (iLength - 1 == iCurrItem) {
			if (pApplication.getCaseCode().length() > 0) {
				UserLog uLog = new UserLog(pApplication.getUserID(),
						pApplication.getCaseCode(), ClassID, 5, qnID);
				dHelper.addUserLog(uLog);
			}
			new AlertDialog.Builder(mContext).setTitle("提示").setMessage(
			"恭喜您一百分！").setPositiveButton("確定",
			new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					finish();
				}
			}).show();
			//finish();
		} else {//沒回答完畢繼續回答直到完畢
			iCurrItem++;
			showQuestion();
		}
	}

	/**
	 * 顯示小測驗問題及選項
	 */
	private void showQuestion() {
		question = questionList.get(iCurrItem);//當前測驗的問題
		tvQuestionTitle.setText(String.format("%d、%s", question.getNo(),
				question.getSubj()));//取得問題標題
		optionList = question.getOptions();

		//顯示問題的相關數據
		rbChoose1.setChecked(false);
		rbChoose2.setChecked(false);
		rbChoose3.setChecked(false);
		rbChoose4.setChecked(false);
		switch (question.getOptionCnt()) {// 判断选择的个数
		case 2:
			rbChoose1.setText(optionList.get(0).getDesc());
			rbChoose2.setText(optionList.get(1).getDesc());
			rbChoose3.setVisibility(View.GONE);
			rbChoose4.setVisibility(View.GONE);
			break;
		case 3:
			rbChoose1.setText(optionList.get(0).getDesc());
			rbChoose2.setText(optionList.get(1).getDesc());
			rbChoose3.setText(optionList.get(2).getDesc());
			rbChoose4.setVisibility(View.GONE);
			break;
		case 4:
			rbChoose1.setText(optionList.get(0).getDesc());
			rbChoose2.setText(optionList.get(1).getDesc());
			rbChoose3.setText(optionList.get(2).getDesc());
			rbChoose4.setText(optionList.get(3).getDesc());
			break;
		default:
			break;
		}
		if (iLength - 1 == iCurrItem) {
			// btnNext.setText("确定");
			btnNext.setBackgroundResource(R.drawable.btncomplete);
		} else {
			btnNext.setBackgroundResource(R.drawable.btnnext);
			// btnNext.setText("下一题");
		}
	}

	@Override
	// 监听退出键
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		
		dHelper = null;
		pApplication = null;
		mContext = null;
		tvQuestionTitle = null;
		btnNext = null;
		questionList = null;// 問題數據源List
		rgChoose = null;
		rbChoose1 = null;
		rbChoose2 = null;
		rbChoose3 = null;
		rbChoose4 = null;
		tvInfo = null;
		question = null;
		optionList = null;
	}
	
	
}
